package server.controller.api;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import plug.result.RetResponse;
import plug.result.RetResult;
import server.dao.ApplyDao;
import server.dao.UserBlanceDao;
import server.dao.UserDepositDao;
import server.dao.UsersDao;
import server.model.dto.ApplyDto;
import server.model.entity.*;
import server.model.vo.ApplyVo;
import server.tool.tool.BeanUtil;

import java.math.BigDecimal;
import java.util.LinkedList;
import java.util.List;

/**
 * 创建人:    xvzhe
 * 创建时间:
 * 修改时间:
 * 版本:      v1.0
 */
@RestController
@RequestMapping("v2/api/car")
@Api(value = "对外接口（提现）", tags = {"需登录(提现)"})
@CrossOrigin(origins = "*")
public class AyController {
    @Autowired
    ApplyDao applyDao;
    @Autowired
    UsersDao usersDao;
    @Autowired
    UserBlanceDao userBlanceDao;
    @Autowired
    UserDepositDao depositDao;

    @ApiOperation("提现申请")
    @PostMapping("/apply")
    public RetResult apply(@RequestBody ApplyVo applyVo) {
        if (applyVo.getMoney().subtract(new BigDecimal(100)).compareTo(BigDecimal.ZERO) < 0) {
            return RetResponse.makeErrRsp("提现金额需大于100");
        }
        if (applyVo.getMoney().remainder(new BigDecimal(100)).compareTo(BigDecimal.ZERO) > 0) {
            return RetResponse.makeErrRsp("需提现为100的倍数");
        }
        Apply apply = new Apply();
        apply.setType(Apply.Type.ING);
        apply.setFastMoney(applyVo.getMoney().multiply(new BigDecimal(0.06)));
        apply.setMoney(applyVo.getMoney().subtract(apply.getFastMoney()));
        Users users = (Users) SecurityUtils.getSubject().getSession().getAttribute("Users");
        users = usersDao.findById(users.getId()).get();
        if (applyVo.getIsDefault()) {
            UserDeposit de = depositDao.findByUserIdAndIsDefaultAndStatus(users.getId(), true, true);
            apply.setUserSiteId(de.getId());
        } else if (StringUtils.isNotBlank(applyVo.getDepositId())) {
            apply.setUserSiteId(applyVo.getDepositId());
        } else {
            return RetResponse.makeErrRsp("请选择提现方式！");
        }
        apply.setUserId(users.getId());
        UserBlance userBlance = userBlanceDao.findByTypeAndUserId(UserBlance.Type.RMB, users.getId());
        if (userBlance.getMoney().subtract(applyVo.getMoney()).compareTo(BigDecimal.ZERO) < 0) {
            return RetResponse.makeErrRsp("余额不足");
        }
        userBlance.setFrostMoney(userBlance.getFrostMoney().add(applyVo.getMoney()));
        userBlance.setMoney(userBlance.getMoney().subtract(applyVo.getMoney()));
        applyDao.save(apply);
        return RetResponse.makeOKRsp();
    }

    @ApiOperation("提现记录")
    @GetMapping("/apply")
    public RetResult getApply() {
        Users users = (Users) SecurityUtils.getSubject().getSession().getAttribute("Users");
        List<Apply> apy = applyDao.findByUserIdOrderByCreateTimeDesc(users.getId());
        List<ApplyDto> applyDtos = new LinkedList<>();
        apy.stream().forEach(item -> {
            ApplyDto applyDto = BeanUtil.convertToBean(item, ApplyDto.class);
            applyDto.setUserDeposit(depositDao.findById(item.getUserSiteId()).get());
            applyDtos.add(applyDto);
        });
        return RetResponse.makeOKRsp(applyDtos);
    }
}
